<template>
  <div id="login" class="login">
    <div class="top-header-bg">
      <div class="bg-header"></div>
      <div class="login-name">资讯社交购物平台</div>
      <div class="img-login"></div>
    </div>
    <div class="main-login">
      <input
        type="text"
        id="username"
        name="username"
        placeholder="请填写账号"
        maxlength="11"
        size="11"
        v-model="username"
        style=""
        autocomplete="off"
      />
      <br />
      <input
        type="password"
        id="password"
        name="password"
        placeholder="请填写密码"
        maxlength="16"
        size="16"
        v-model="password"
      />
      <div class="sub-btn">
        <button class="login-btn" @click="login">登录</button>
        <!-- <button class="login-btn" @click="login1">登录</button> -->
      </div>
      <div class="sub-link">
        <div @click="toRegist()">前往注册</div>
        <div @click="toChangePassword()">忘记密码?</div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  created() {
    // 接收来自注册、修改密码后的账号
    this.username = this.$route.query.username;
  },
  methods: {
    toRegist() {
      this.$router.push("/register");
    },
    toChangePassword() {
      this.$router.push({
        path: "/changeWord",
      });
    },
    login1() {
      this.$router.push({
        path: "/idssp",
      });
    },
    // 提交登录
    // 查
    login() {
      // 不能让管理员登录
      if (this.username == "admin") {
        Toast("管理员不可以登录！！！");
        return;
      }
      if (!this.username || !this.password) {
        Toast("用户名和密码不能为空！！！");
      } else {
        if (this.password.length < 6) {
          Toast("密码不少于6位！！！");
        } else {
          let params = {
            username: this.username,
            password: this.password,
          };
          this.$http.post("/login", params).then((res) => {
            if (res.data.errcode === 200) {
              let data = res.data.message[0];
              localStorage.setItem("UserInfo", window.JSON.stringify(data));
              let distRouter = "/idssp"; // 用户界面 2
              let identity = data.identity;
              localStorage.setItem("identityResult", identity);
              // 登录成功，则跳转到首页，并传入用户名
              this.$router.push({
                path: distRouter,
                query: { username: this.username },
              });
              Toast("欢迎" + this.username);
              this.$router.go(0);
            } else {
              Toast("用户名或者密码错误！！！");
            }
          });
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
@mainColor: #20a2ed;
.top-header-bg {
  margin-bottom: 10px;
  position: relative;
  width: 100%;
  height: 140px;
  overflow: hidden;
  .img-login {
    margin: auto;
    position: absolute;
    left: 0;
    top: 20px;
    right: 0;
    width: 78px;
    height: 78px;
    background: url(../assets/img/login/Logo.png) no-repeat;
    background-size: 100% 100%;
  }

  .login-name {
    margin-top: 110px;
    font-size: 23px;
    text-align: center;
  }
  .bg-header {
    position: absolute;
    left: -50%;
    top: -100%;
    width: 200%;
    height: 200%;
    background: url(../assets/img/login/loginBg.png) no-repeat;
    background-size: 100% 100%;
  }
}

.main-login {
  width: 90%;
  margin: 0 auto;
  padding: 15px 25px;
  input {
    width: 100%;
    height: 40px;
    border-radius: 20px;
    font-size: 16px;
    line-height: 40px;
    text-align: center;
  }
  .sub-btn {
    width: 100%;
    button {
      width: 100%;
      height: 45px;
      border-radius: 20px;
      background-color: #00b2e2;
      color: #fff;
    }
  }
  .sub-link {
    margin-top: 20px;
    color: #00b6e8;
    font-size: 15px;
    display: flex;
    justify-content: space-between;
  }
}
.login {
  background: #fff;
  height: 100vh;
}
</style>
